package com.yz.food.cms.controller;

import com.yz.food.cms.common.ServerResponse;
import com.yz.food.cms.pojo.Category;
import com.yz.food.cms.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类
     */
    @RequestMapping("page/category")
    public String category(ModelMap modelMap) {
        List<Category> categories = categoryService.getAll();
        modelMap.addAttribute("categories", categories);
        return "category";
    }

    /**
     * 修改分类
     */
    @RequestMapping("category/updateCategory")
    @ResponseBody
    public ServerResponse updateCategory(Category category) {
        int result  = categoryService.updateCategory(category);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    /**
     * 根据id删除
     */
    @RequestMapping("category/deleteById")
    @ResponseBody
    public ServerResponse deleteById(@RequestParam("categoryId") Integer categoryId) {
        int result = categoryService.deleteById(categoryId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }
}
